package com.mymall.serve.service;

import cn.hutool.core.util.XmlUtil;
import com.mymall.serve.config.redission.annotation.Lock;
import com.mymall.serve.utils.CacheUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 缓存处理service
 *
 * @author weiwei
 * @since 2020-04-27 14:52
 **/
@Service
public class CacheService {
    @Autowired
    private CacheUtil cacheUtil;

    @Lock(keys = "(#key).concat(':').concat(#value)",keyConstant = "incrementScore")
    public void updateRankEventListener(String key, String value, double score) {
        Set<String> ids = cacheUtil.range(key, 0, -1);
        // 查看排行榜，不存在值则添加，存在则点击加1
        if (CollectionUtils.isEmpty(ids) || !ids.contains(String.valueOf(value))){
            cacheUtil.zadd(key,value,score);
        }else {
            cacheUtil.incrScore(key,value,score);
        }
    }

    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><verify><versionId>01</versionId><custId>6000003635</custId><ordId>UNBD2020072300000001</ordId><transType>STD_VERI</transType><merPriv></merPriv><jsonStr>{\\\"CARD_ID\\\":\\\"6217000010074602668\\\",\\\"CERT_NAME\\\":\\\"魏巍\\\",\\\"PROD_ID\\\":\\\"CARD5\\\",\\\"CERT_ID\\\":\\\"140212199302100014\\\",\\\"MP\\\":\\\"18600197436\\\",\\\"CARD_TYPE\\\":\\\"1\\\"}</jsonStr><respCode>000</respCode><respDesc>一致</respDesc><resTxnId>202007230152326936</resTxnId><macStr>28F3773E1ED359BB08B8D7E6B0525544</macStr></verify>";
        Object o = XmlUtil.readObjectFromXml(xml);
        System.out.println(o);
    }
}
